/**
 * Created by HAIWEI-LC on 2019/10/23
 */
package com.hw.paltfrom.service.system;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.exception.HWSystemException;
import com.hw.paltfrom.mapper.HwBaseCountycoreMapper;
import com.hw.paltfrom.pojo.HwBaseCountycore;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.LanguageUtil;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.BaseCountycoreVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CountycoreService {

    @Autowired
    HwBaseCountycoreMapper baseCountycoreMapper;

    //增加治超中心
    public Map<String, Object> add(BaseCountycoreVO vo){
        //验证志超中心编码是否存在
//        Wrapper<HwBaseCountycore> wrapper = new EntityWrapper<>();
//        wrapper.eq("F_CountyCoreCode",vo.getfCountycorecode()).or().eq("F_CountyCoreName",vo.getfCountycorename());
//        List<HwBaseCountycore> exists = baseCountycoreMapper.selectList(wrapper);
//        if (!exists.isEmpty()){
//            throw new HWSystemException(HWSystemCode.FAIL,"治超中心编码已存在");
//        }

        //验证治超中心名称是否存在
        Wrapper<HwBaseCountycore> wrapper = new EntityWrapper<>();
        wrapper.eq("F_CountyCoreName",vo.getfCountycorename());
        List<HwBaseCountycore> exists = baseCountycoreMapper.selectList(wrapper);
        if (!exists.isEmpty()){
            return ResponeResultUtil.failNotice(HWSystemCode.FAIL,"治超中心名称已存在");
        }

        //初始化基础数据
        HwBaseCountycore hwBaseCountycore = new HwBaseCountycore();
        hwBaseCountycore.setfCountycoreid(RandomNumberUtil.getNumber());
        hwBaseCountycore.setfAreaid(vo.getfAreaid());
        hwBaseCountycore.setfCountycorecode(RandomNumberUtil.getCode());
        hwBaseCountycore.setfCountycorename(vo.getfCountycorename());
        hwBaseCountycore.setfQuickquery(LanguageUtil.toHanyuPinyin(vo.getfCountycorename()));
        hwBaseCountycore.setfSimplespelling(vo.getfSimplespelling());
        hwBaseCountycore.setfLongitude(vo.getfLongitude());
        hwBaseCountycore.setfLatitude(vo.getfLatitude());
        hwBaseCountycore.setfAddress(vo.getfAddress());
        hwBaseCountycore.setfManager(vo.getfManager());
        hwBaseCountycore.setfTel(vo.getfTel());
        hwBaseCountycore.setfPic(vo.getfPic());
        hwBaseCountycore.setfSortcode(vo.getfSortcode());
        hwBaseCountycore.setfDeletemark(0);
        hwBaseCountycore.setfEnabledmark(1);
        hwBaseCountycore.setfDescription(vo.getfDescription());
        hwBaseCountycore.setfCreatedate(new Date());
        hwBaseCountycore.setfCreateuserid(vo.getfCreateuserid());
        hwBaseCountycore.setfCreateusername(vo.getfCreateusername());

        //数据入库
        Integer insert = baseCountycoreMapper.insert(hwBaseCountycore);
        if (insert != 1) {
            return ResponeResultUtil.fail(HWSystemCode.FAIL);
        }

        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }

    //查找治超中心
    public Map<String, Object> findCountyCore(BaseCountycoreVO vo){

        //创建条件查询对象
        Wrapper<HwBaseCountycore> wrapper = new EntityWrapper<>();

        //根据所传参数确定查询内容,自动分页,删除标记必须为0,模糊查询;
         wrapper.eq(StringUtils.isNotBlank(vo.getfAreaid()),"F_AreaId",vo.getfAreaid())
                 .like(StringUtils.isNotBlank(vo.getfCountycorename()),"F_CountyCoreName",vo.getfCountycorename())
                 .eq("F_DeleteMark", "0").orderBy("F_SortCode");
         //创建分页条件
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<HwBaseCountycore> hwBaseCountycoreList = baseCountycoreMapper.selectList(wrapper);
        PageInfo<HwBaseCountycore> info = new PageInfo<>(hwBaseCountycoreList);
        return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS,info);
    }

    //删除治超中心,不做实际删除,0 for 未删除,1 for 删除.
    public Map<String, Object> deleteById(String id, String fModifyuserid, String fModifyusername) {

            //查出id所对应超限站
            HwBaseCountycore hwBaseCountycore= baseCountycoreMapper.selectById(id);
            if (hwBaseCountycore == null){
                return ResponeResultUtil.fail(HWSystemCode.PARAMS_ERROR);
            }
            //标记删除
            hwBaseCountycore.setfDeletemark(1);
            hwBaseCountycore.setfModifydate(new Date());
            hwBaseCountycore.setfModifyuserid(fModifyuserid);
            hwBaseCountycore.setfModifyusername(fModifyusername);
            Integer update = baseCountycoreMapper.updateById(hwBaseCountycore);
            //
            //删除失败
            if (update != 1){
                return ResponeResultUtil.failNotice(HWSystemCode.FAIL,hwBaseCountycore.getfCountycorename()+"删除失败");
            }

        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }

    //更新治超中心
    public Map<String, Object> updateCountrycore(HwBaseCountycore hwBaseCountycore) {
        //根据id查出修改的治超中心
        if (StringUtils.isNotBlank(hwBaseCountycore.getfCountycorename())){
            HwBaseCountycore hwBaseCountycore_old = baseCountycoreMapper.selectById(hwBaseCountycore.getfCountycoreid());

            //如果查询治超中心名称修改了,检查数据库中是否有重复名称
            if (!hwBaseCountycore_old.getfCountycorename().equals(hwBaseCountycore.getfCountycorename())){
                EntityWrapper<HwBaseCountycore> wrapper = new EntityWrapper<>();
                wrapper.eq("F_CountyCoreName",hwBaseCountycore.getfCountycorename());
                List<HwBaseCountycore> list = baseCountycoreMapper.selectList(wrapper);
                if (!list.isEmpty()){
                    return ResponeResultUtil.failNotice(HWSystemCode.FAIL,"治超中心名称已存在");
                }
                //更新治超中心名快搜,简拼
                hwBaseCountycore.setfQuickquery(LanguageUtil.toHanyuPinyin(hwBaseCountycore.getfCountycorename()));
                hwBaseCountycore.setfSimplespelling(hwBaseCountycore.getfSimplespelling());
            }
        }


        hwBaseCountycore.setfModifydate(new Date());
        baseCountycoreMapper.updateById(hwBaseCountycore);

        return ResponeResultUtil.success(HWSystemCode.SUCCESS);
    }
}
